package com.hgyc.mom.wms.service.impl;

import com.hgyc.mom.tool.strategy.AutoCodeUtil;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.wms.mapper.WmsStockInMapper;
import com.hgyc.mom.wms.entity.WmsStockIn;
import com.hgyc.mom.wms.entity.WmsStockInDetail;
import com.hgyc.mom.wms.service.WmsStockInDetailService;
import com.hgyc.mom.wms.vo.WmsStockInVO;
import com.hgyc.mom.wms.service.WmsStockInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 入库单Service业务层实现
 *
 * @author fwj
 * @since 2025-10-08
 */
@Slf4j
@Service
public class WmsStockInServiceImpl extends ServiceImpl<WmsStockInMapper, WmsStockIn> implements WmsStockInService {

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Autowired
    private WmsStockInDetailService wmsStockInDetailService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveStockIn(WmsStockInVO wmsStockInVO) {
        wmsStockInVO.setCode(autoCodeUtil.genSerialCode("wms_stock_in_code",null));
        save(wmsStockInVO);

        //新增入库单明细
        List<WmsStockInDetail> stockInDetails = wmsStockInVO.getAddStockInDetails();
        if (!CollectionUtils.isEmpty(stockInDetails)) {
            stockInDetails.forEach(item -> {
                item.setId(null);
                item.setStockInId(wmsStockInVO.getId());
            });
            wmsStockInDetailService.saveBatch(stockInDetails);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStockIn(WmsStockInVO wmsStockInVO) {
        // TODO 其他逻辑
        updateById(wmsStockInVO);

        //删除入库单明细
        if (!CollectionUtils.isEmpty(wmsStockInVO.getDeleteStockInDetailIds())) {
            wmsStockInDetailService.removeBatchByIds(wmsStockInVO.getDeleteStockInDetailIds());
        }

        //新增入库单明细
        List<WmsStockInDetail> stockInDetails = wmsStockInVO.getAddStockInDetails();
        if (!CollectionUtils.isEmpty(stockInDetails)) {
            stockInDetails.forEach(item -> {
                item.setId(null);
                item.setStockInId(wmsStockInVO.getId());
            });
            wmsStockInDetailService.saveBatch(stockInDetails);
        }

        //更新入库单明细
        if (!CollectionUtils.isEmpty(wmsStockInVO.getUpdateStockInDetails())) {
            List<WmsStockInDetail> updateStockInDetails = wmsStockInVO.getUpdateStockInDetails();
            wmsStockInDetailService.updateBatchById(updateStockInDetails);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeStockIn(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除入库单明细
        wmsStockInDetailService.deleteBatchByParentIds(ids);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<WmsStockIn> wmsStockInList, boolean updateSupport) {
        if (wmsStockInList == null || wmsStockInList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + wmsStockInList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + wmsStockInList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("入库单导入失败", e);
            throw new BizException("入库单导入失败：" + e.getMessage());
        }
    }
}
